<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class worker_model extends CI_Model {
	//모델 생성자 호출
	function __construct(){
		parent::__construct();
		$this->load->helper('file');
	}
	
	/**
	 * 설교방송 내용 조회
	 *
	 * @access	public
	 * @return	array
	 */
	function select($id){
		$this->db->select("*");
	  	$this->db->where('id', $id);
	  	$query = $this->db->get('ch_worker');
	  	//echo $this->db->last_query();
	  	$row = $query->row_array();
	  	
	  	if($row){
	  		$data['dbjob'] = 'u';
	  		$data['id'] = $id; 
	  		$data['title'] = $row['title'];
	  		$data['duty'] = $row['duty'];
	  		$data['name'] = $row['name'];
	  		$data['photo'] = $row['photo'];
	  		if($row['tel']){
				$tel = explode('-',$row['tel']);
				$data['tel1'] = $tel[0];
				$data['tel2'] = $tel[1];
				$data['tel3'] = $tel[2];
			}
			
	  		if($row['hp']){
				$hp = explode('-',$row['hp']);
				$data['hp1'] = $hp[0];
				$data['hp2'] = $hp[1];
				$data['hp3'] = $hp[2];
			}
			
			if($row['email']){
				$arrMail = explode('@',$row['email']);
				$data['mail_id'] = $arrMail[0];
				$data['mail_domain'] = $arrMail[1];
			}else{
				$data['mail_id'] = null;
				$data['mail_domain'] = null;
			}
	  	}else{
	  		$data['dbjob'] = 'i';
	  		$data['id'] = null; 
	  		$data['title'] = null;
	  		$data['duty'] = null;
	  		$data['name'] = null;
	  		$data['photo'] = null;
	  		$data['mail_id'] = null;
			$data['mail_domain'] = null;
	  		$data['tel1'] = null;
	  		$data['tel2'] = null;
	  		$data['tel3'] = null;
	  		$data['hp1'] = null;
	  		$data['hp2'] = null;
	  		$data['hp3'] = null;
	  	}
	  	return $data;
	}
	
	/**
	 * 설교방송 검색조건
	 *
	 * @access	public
	 * @return	query where절 구문
	 */
	function list_where($param){
		$sch_duty = $param['sch_duty'];
		if($sch_duty){
			return $this->db->where(array('duty'=>$sch_duty));
		}else{
			return null;
		}
	}
	
	/**
	 * 설교방송 db 처리
	 *
	 * @access	public
	 * @return	void
	 */
	function dbjob($data){
		$dbjob = $this->input->post('dbjob');
		$id = $this->input->post('id');
		switch($dbjob){
			case('i'):
				$this->db->insert('ch_worker',$data);
			break;
			case('u'):
				$this->db->where('id',$id);
				$this->db->update('ch_worker',$data);
			break;
			case('d'):
				$this->db->delete('ch_worker',$data);
			break;
		}
	}
	
	/**
	 * 설교방송 총갯수
	 *
	 * @access	public
	 * @return	integer
	 */
	function list_total($param){
		$this->list_where($param);
		$count = $this->db->count_all_results('ch_worker');
		return $count;
	}
	
	/**
	 * 설교방송 리스트
	 *
	 * @access	public
	 * @return	result
	 */
	function worker_list($param){
		$page = $param['page'];
		$listnum = $param['listnum'];
		$offset = $listnum*($page-1);
		$this->list_where($param);
		$this->db->order_by('id DESC');
		$query = $this->db->get('ch_worker',$listnum,$offset);
		//echo $this->db->last_query();
		return $query->result();
	}
}
?>